<?php

namespace App\Console\Commands;

use App\Models\PowerLog;
use App\Models\RentCloth;
use App\Models\Wallet;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class RentProfit extends Command
{

    protected $signature = 'profit:rentcloth';

    protected $description = '每天租衣收益的算力';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $rentcloths = RentCloth::where('status', 1)->get();
        $user_arr = $rentcloths->pluck('user_id');
        DB::beginTransaction();
        try {
            Wallet::whereIn('user_id', $user_arr)->increment('computing_power', 20);
            foreach ($user_arr as $value) {
                PowerLog::add_log($value, 7, 20, 1);
            }
            log::channel('profit')->info('wallet',[
                'user_id'=>$user_arr,               
            ]);
            DB::commit();
        } catch (\Exception $exception) {
            Log::channel('profit')->error('profit:rentcloth error', [
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
            ]);
            DB::rollBack();
        }
    }
}
